package com.rzx.dim4status.controller;

import com.rzx.dim4status.service.PlaceService;
import com.rzx.dim4status.utils.Result;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value = "/globalState")
public class GlobalController {//存储是与否,仅有一个无需添加

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    PlaceService placeService;

    @PostMapping("/updateGlobalDecide")
    public Result setGlobalStateDecide(@RequestBody Map<String,String> map) {
        String decide = map.get("decide");
        if (Objects.equals(decide, "run") || Objects.equals(decide, "stop") ) {
            stringRedisTemplate.opsForValue().set("global:dim4-server-status", decide);
            placeService.addPlaceComputer();
            return Result.ok("更新成功",map);
        }
        return Result.fail("更新失败");
    }
    @GetMapping("/getGlobalState")//查
    public Result getGlobalState(){
        Map<String,String> globalMap = new HashMap<>();
        globalMap.put("globalId","global:dim4-server-status");
        globalMap.put("globalState",stringRedisTemplate.opsForValue().get("global:dim4-server-status"));
        placeService.addPlaceComputer();
        return Result.ok("查找成功",globalMap);
    }
}
